
package net.lab0.fractal.conf;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.InvalidPropertiesFormatException;

import net.lab0.fractal.enums.DefaultsEnum;
import net.lab0.tools.cfg.ConfigurationRoot;

import org.apache.log4j.Logger;


public class ApplicationConfiguration
{
    private static ConfigurationRoot configurationRoot;
    private static Logger            logger = Logger.getLogger(ApplicationConfiguration.class);
    
    private static PathConfiguration pathConfiguration;
    
    private ApplicationConfiguration()
    {
        
    }
    
    public static void init()
    throws InvalidPropertiesFormatException, FileNotFoundException, IOException, URISyntaxException
    {
        if (configurationRoot == null)
        {
            configurationRoot = new ConfigurationRoot(DefaultsEnum.DEFAULT_CONFIGURATION_FILE, true);
        }
        logger.info("Application configuration initalized");
    }
    
    public static void reset()
    throws InvalidPropertiesFormatException, FileNotFoundException, IOException, IllegalArgumentException, URISyntaxException
    {
        PathConfiguration pathConfiguration = new PathConfiguration(new File("./cfg/path.xml"), false);
        
        pathConfiguration.reset();
        
        configurationRoot = new ConfigurationRoot(DefaultsEnum.DEFAULT_CONFIGURATION_FILE, false);
        configurationRoot.addConfiguration("path", pathConfiguration);
    }
    
    /**
     * Must call init first at least once
     * 
     * @return the <code>configurationRoot</code> singleton
     */
    public static synchronized ConfigurationRoot getConfigurationRoot()
    {
        return configurationRoot;
    }
    
    public static PathConfiguration getPathConfiguration()
    {
        if (pathConfiguration == null)
        {
            pathConfiguration = new PathConfiguration(configurationRoot.getConfiguration("path"));
        }
        return pathConfiguration;
    }
    
    public static void main(String[] args)
    throws FileNotFoundException, IOException, URISyntaxException
    {
        ApplicationConfiguration.reset();
    }
}
